using System;
using System.Collections;
using System.Linq;
using System.Web.Mvc;
using JinTai.MemorialTablets;
using JinTai.MemorialTablets.Interfaces;
using Ornament.MVCWebFrame.Models;
using Ornament.Web.Mvc;
using Ornament.Web;

namespace Ornament.MVCWebFrame.Controllers
{
    public class MemorialTabletController : Controller
    {
        //
        // GET: /MemorialTables/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /MemorialTables/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        [JsonContainerFilter]
        public ActionResult Search(JsonContainer data)
        {
            
            IMemorialTabletDao dao = OrnamentContext.Current.MemorialTablesFactory.CreateMemorialTabletDao();
            var search = new MemorialTabletSearcher();
            search.HomePhone = data.ToString("homePhone");
            search.Mobile = data.ToString("mobile");
            search.Number = data.ToString("number");
            search.PageIndex = data.ToInt32("page") - 1;
            search.PageSize = data.ToInt32("rows");
            var result = dao.Search(search);
            var list = new ArrayList(result.Count);
            foreach (var tablet in result)
            {
                Contact c = tablet.Contacts.First();
                var o = new
                            {
                                id = tablet.Id,
                                cell = new[]
                                {
                                    tablet.Ancestors.First().Name,
                                    tablet.AreaType.ToString(),
                                    c.Mobile,
                                    c.HomePhone,
                                    c.Address.ToString()
                                }
                            };
                list.Add(o);
            }

            var dataJson = new
            {
                total = 1,
                page = search.PageIndex + 1,
                records = search.TotalNumber,
                rows = list
            };
            return Json(dataJson);
        }

        //
        // GET: /MemorialTables/Create

        public ActionResult Create()
        {
            ViewData["AreaType"] = from enumItem in EnumHelper.GetDescriptionList<AreaType>()
                                   select new SelectListItem { Text = enumItem.Key, Value = enumItem.Value.ToString() };

            return View();
        }

        //
        // POST: /MemorialTables/Create

        [JsonContainerFilter]
        public ActionResult Save(JsonContainer data)
        {
            try
            {
                IMemorialTabletDao dao = OrnamentContext.Current.MemorialTablesFactory.CreateMemorialTabletDao();
                MemorialTablet obj = data.Contains("id") ? dao.Get(data.ToGuid("Id")) : new MemorialTablet();
                obj.Number = data.ToString("Number");
                obj.Content = data.ToString("Content");
                obj.Memo = data.ToString("Memo");
                obj.AreaType = (AreaType)Enum.Parse(typeof(AreaType), data.ToString("AreaType"));
                foreach (JsonContainer container in data.ToArray<JsonContainer>("Ancestors"))
                {
                    DateTime birthday = DateTime.Parse(container.ToString("Birthday").Trim());
                    DateTime deadday = DateTime.Parse(container.ToString("Deadday").Trim());
                    var ancestor = new Ancestor
                                       {
                                           Name = container.ToString("Name"),
                                           ShowContent = container.ToString("ShowContent"),
                                           Birthday = birthday,
                                           Deadday = deadday
                                       };
                    obj.Ancestors.Add(ancestor);
                }

                obj.Contacts.Clear();
                foreach (JsonContainer container in data.ToArray<JsonContainer>("Contacts"))
                {
                    Contact contact = String.IsNullOrEmpty(container.ToString("Id"))
                                          ? new Contact()
                                          : OrnamentContext.Current.MemorialTablesFactory.CreateContactDao().Get(
                                                new Guid(container.ToString("Id")));
                    contact.Address = new Addressbook
                                          {
                                              Address = container.ToString("Address.Address"),
                                              City = container.ToString("Address.City"),
                                              District = container.ToString("Address.District")
                                          };
                    contact.HomePhone = container.ToString("HomePhone");
                    contact.Mobile = container.ToString("Mobile");
                    contact.Name = container.ToString("Name");
                    obj.Contacts.Add(contact);
                }
                dao.SaveOrUpdate(obj);
                return Json(true);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
    }
}